package com.dlc.commonlibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.v4.content.res.ResourcesCompat;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.dlc.commonlibrary.R;
import com.dlc.commonlibrary.utils.DialogUtil;
import java.util.ArrayList;

public class ActionSheetDialog extends Dialog {

    private Context mContext;

    private TextView mTvCancel;
    private LinearLayout mLlContent;
    private final ArrayList<String> mItemList;
    private OnSheetItemClickListener mOnSheetItemClickListener;
    private int mItemColor;
    private float mItemTextSize;
    private float mItemHeight;
    private float mLineHeight;
    private int mLineColor;

    private ActionSheetDialog(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        mContext = context;
        mItemList = new ArrayList<>();
        initDialog();
        initParams();
    }

    private void initParams() {
        Resources resources = mContext.getResources();
        mItemColor = ResourcesCompat.getColor(resources, R.color.action_sheet_item_color, null);
        mItemTextSize = resources.getDimension(R.dimen.action_sheet_item_text_size);
        mItemHeight = resources.getDimension(R.dimen.action_sheet_item_height);
        mLineHeight = resources.getDimension(R.dimen.action_sheet_line_height);
        mLineColor = ResourcesCompat.getColor(resources, R.color.action_sheet_line_color, null);
    }

    private void initDialog() {
        // 获取Dialog布局
        setContentView(R.layout.dialog_action_sheet);
        DialogUtil.adjustDialogLayout(this, true, false);

        mLlContent = (LinearLayout) findViewById(R.id.ll_content);
        mTvCancel = (TextView) findViewById(R.id.tv_cancel);
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    public static ActionSheetDialog.Builder newBuilder(Context context) {
        return new Builder(context);
    }

    public static class Builder {

        private ActionSheetDialog mDialog;

        public Builder(Context context) {
            mDialog = new ActionSheetDialog(context);
        }

        public Builder setOnSheetItemClickListener(
            OnSheetItemClickListener onSheetItemClickListener) {
            mDialog.setOnSheetItemClickListener(onSheetItemClickListener);
            return this;
        }

        public Builder addItem(String itemText) {
            mDialog.addItem(itemText);
            return this;
        }

        public Builder addItem(int stringResId) {
            mDialog.addItem(stringResId);
            return this;
        }

        public Builder setItemColor(int color) {
            mDialog.setItemColor(color);
            return this;
        }

        public Builder setCancelable(boolean flag) {
            mDialog.setCancelable(flag);
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean cancel) {
            mDialog.setCanceledOnTouchOutside(cancel);
            return this;
        }

        public Builder setCancelText(String cancelText) {
            mDialog.setCancelText(cancelText);
            return this;
        }

        public Builder setCancelText(int stringResId) {
            mDialog.setCancelText(stringResId);
            return this;
        }

        public ActionSheetDialog build() {
            mDialog.setSheetItems();
            return mDialog;
        }

        public void show() {
            build().show();
        }
    }

    public interface OnSheetItemClickListener {
        /**
         * 点击条目
         *
         * @param which 条目位置，从0开始
         */
        void onClick(int which);
    }

    public OnSheetItemClickListener getOnSheetItemClickListener() {
        return mOnSheetItemClickListener;
    }

    public void setOnSheetItemClickListener(OnSheetItemClickListener onSheetItemClickListener) {
        mOnSheetItemClickListener = onSheetItemClickListener;
    }

    private void addItem(String itemText) {
        mItemList.add(itemText);
    }

    private void addItem(int stringResId) {
        mItemList.add(mContext.getString(stringResId));
    }

    private void setItemColor(int color) {
        mItemColor = color;
    }

    private void setCancelText(String cancelText) {
        mTvCancel.setText(cancelText);
    }

    private void setCancelText(int stringResId) {
        mTvCancel.setText(stringResId);
    }

    /** 设置条目布局 */
    private void setSheetItems() {
        if (mItemList.size() <= 0) {
            return;
        }

        int size = mItemList.size();

        // 循环添加条目
        for (int i = 0; i < size; i++) {
            final int index = i;
            String itemText = mItemList.get(i);

            TextView textView = new TextView(mContext);
            textView.setText(itemText);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mItemTextSize);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(mItemColor);

            LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    (int) mItemHeight);

            textView.setLayoutParams(layoutParams);

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (mOnSheetItemClickListener != null) {
                        mOnSheetItemClickListener.onClick(index);
                    }
                }
            });

            mLlContent.addView(textView);
            // 背景图片
            if (size == 1) {
                textView.setBackgroundResource(R.drawable.selector_action_sheet_single);
            } else {
                if (i == 0) {
                    textView.setBackgroundResource(R.drawable.selector_action_sheet_top);
                    mLlContent.addView(newLine());
                } else if (i < size - 1) {
                    textView.setBackgroundResource(R.drawable.selector_action_sheet_middle);
                    mLlContent.addView(newLine());
                } else {
                    textView.setBackgroundResource(R.drawable.selector_action_sheet_bottom);
                }
            }
        }
    }

    private View newLine() {
        View view = new View(mContext);
        LinearLayout.LayoutParams layoutParams =
            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                (int) mLineHeight);
        view.setBackgroundColor(mLineColor);
        view.setLayoutParams(layoutParams);
        return view;
    }
}
